package com.web.hawaste.shiro;

import com.web.hawaste.entity.SysRole;
import com.web.hawaste.entity.SysUser;
import com.web.hawaste.service.ISysResourceService;
import com.web.hawaste.service.ISysRoleService;
import com.web.hawaste.service.ISysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.ArrayList;
import java.util.List;

/**
 * YangSuzy 软件工作室
 * 类名: MyRealm
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/10/12 23:37
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysResourceService sysResourceService;

    /**
     * 方法名: doGetAuthenticationInfo
     * 描述: 用户认证
     *
     * @param token
     * @return org.apache.shiro.authc.AuthenticationInfo
     * @date 2022/10/12 23:38
     * @auther YangSuzy
     **/
    @Lazy
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        System.out.println("用户认证：" + username);
        SysUser user = sysUserService.findUserByUsername(username);
        if (user == null) {
            return null;
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getUsername()), "MyRealm");
        return info;
    }

    /**
     * 方法名: doGetAuthorizationInfo
     * 描述: 角色权限认证
     *
     * @param principal
     * @return org.apache.shiro.authz.AuthorizationInfo
     * @date 2022/10/12 23:38
     * @auther YangSuzy
     **/
    @Lazy
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        SysUser user = (SysUser) principal.getPrimaryPrincipal();
        //角色集合
        List<String> roles = new ArrayList<>();
        //权限集合
        List<String> resourceList = new ArrayList<>();

        //遍历角色列表
        sysRoleService.selectByUid(user.getId()).forEach(sysRole -> {
            if (!roles.contains(sysRole.getName())) {
                roles.add(sysRole.getName());
            }
        });
        System.out.println("角色集合：" + roles);
        //遍历权限列表
        sysResourceService.selectByUid(user.getId()).forEach(sysResource -> {
            if (!resourceList.contains(sysResource.getPermissionStr())) {
                resourceList.add(sysResource.getPermissionStr());
            }
        });
        System.out.println("权限集合：" + resourceList);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roles);
        info.addStringPermissions(resourceList);
        return info;
    }
}
